package com.jiazhong.basic.pack0316.thread;

public class DemoE {

    private int num = 0;
    private Object obj = new Object();

    /*public synchronized void change() {
        String name = Thread.currentThread().getName();
        System.out.println(name + "准备求和运算");
        num = num + 1;
        System.out.println(name + "当前值是：" + num);
    }*/
    /*public synchronized void change() {
        String name = Thread.currentThread().getName();
        System.out.println(name + "准备求和运算");
        num = num + 1;
        try {
            Thread.sleep(10000); // 1s===1000毫秒ms
        } catch (InterruptedException ex) {
            throw new RuntimeException(ex);
        }
        System.out.println(name + "当前值是：" + num);
    }*/
    public void change() {
        String name = Thread.currentThread().getName();
        System.out.println(name + "准备求和运算");

        synchronized (Object.class) {
            try {
                Thread.sleep(10000); // 1s===1000毫秒ms
            } catch (InterruptedException ex) {
                throw new RuntimeException(ex);
            }
            num = num + 1;
            System.out.println(name + "当前值是：" + num);
        }
    }

    /* public  void change() {
         System.out.println("准备求和运算");
         synchronized(Object.class){
             num = num + 1;
             String name = Thread.currentThread().getName();
             System.out.println(name + "当前值是：" + num);
         }
     }*/
    public int getNum() {
        return num;
    }
}
